package com.spera.genutil;

import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;

public class IdentifyCodeUtil {
	
	public static String IdentifyCode = "IdentifyCode";
	
	/**
	 * 判断是否需要校验验证码
	 * @param username
	 * @param JedisCache
	 * @return
	 */
	public static boolean checkNeedIdentify(String username,JedisCache jedisCache){
		boolean hasin =jedisCache.exists(IdentifyCode+"_"+username);
		if(hasin){
			String logTimes = jedisCache.lindex(IdentifyCode+"_"+username, 0);//登录次数
			if(Integer.parseInt(logTimes)>1){
				jedisCache.lset(IdentifyCode+"_"+username, 0, (Integer.parseInt(logTimes)+1)+"");//增加次数
				return true;
			}else{
				jedisCache.lset(IdentifyCode+"_"+username, 0, (Integer.parseInt(logTimes)+1)+"");//增加次数
				return false;
			}
		}else{
			jedisCache.rpush(IdentifyCode+"_"+username, "0");//次数
			jedisCache.rpush(IdentifyCode+"_"+username, "0000");//次数
			jedisCache.expire(IdentifyCode+"_"+username, 60*5);
			return false;
		}
	}
	
	/**
	 * 检测是否需要验证码的状态
	 * @param username
	 * @param JedisCache
	 * @return
	 */
	public static String checkIdentifyStatus(String username,JedisCache jedisCache){
		boolean hasin =jedisCache.exists(IdentifyCode+"_"+username);
		if(hasin){
			String logTimes = jedisCache.lindex(IdentifyCode+"_"+username, 0);//登录次数
			jedisCache.expire(IdentifyCode+"_"+username, 60*5);
			if(Integer.parseInt(logTimes)>1){
				return "1";
			}else{
				return "0";
			}
		}else{
			return "0";
		}
		
	}
	
	/**
	 * 检查验证码是否正确，是否可进行下步操作
	 * @param username
	 * @param JedisCache
	 * @param valnum
	 * @return
	 */
	public static boolean checkIdentifyCode(String username,JedisCache jedisCache,String valnum){
		if(!ApplicationUtils.isNotNull(valnum)){
			return false;
		}
		if(jedisCache.llen(IdentifyCode+"_"+username)<=1){
			return true;
		}
		String yourValNum = jedisCache.lindex(IdentifyCode+"_"+username, 1);
		if(yourValNum.equalsIgnoreCase(valnum)){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 校验失败或登录失败
	 * @param username
	 * @param JedisCache
	 * @param valnum
	 * @return
	 */
	public static String logFail(String username,JedisCache jedisCache){
		if(!ApplicationUtils.isNotNull(username)){
			return null;
		}
		boolean hasin =jedisCache.exists(IdentifyCode+"_"+username);
		int[] arrNum = ApplicationUtils.randomArray(0, 9, 4);
		String valnum = String.valueOf(arrNum[0])+String.valueOf(arrNum[1])+
				String.valueOf(arrNum[2])+String.valueOf(arrNum[3]);
		if(hasin){//验证次数已经存在
			String logTimes = jedisCache.lindex(IdentifyCode+"_"+username, 0);//登录次数
			jedisCache.lset(IdentifyCode+"_"+username, 0, (Integer.parseInt(logTimes)+1)+"");//次数
			jedisCache.lset(IdentifyCode+"_"+username, 1,valnum);
		}else{
			jedisCache.rpush(IdentifyCode+"_"+username, "0");
			jedisCache.rpush(IdentifyCode+"_"+username, valnum);
			jedisCache.expire(IdentifyCode+"_"+username, 60*5);
		}
		return valnum;
	}
	
	/**
	 * 校验成功并登录成功后删除登录失败次数验证
	 * @param username
	 * @param JedisCache
	 * @return
	 */
	public static void logSuccess(String username,JedisCache jedisCache){
		jedisCache.del(IdentifyCode+"_"+username);
	}
	
	/**
	 * 获取校验码
	 * @param username
	 * @param JedisCache
	 * @return
	 */
	public static String getValNum(String username,JedisCache jedisCache){
		boolean hasin =jedisCache.exists(IdentifyCode+"_"+username);
		if(hasin){//验证次数已经存在
			return jedisCache.lindex(IdentifyCode+"_"+username, 1);
		}else{
			return null;
		}
	}
}
